﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;  //引用命名空间


namespace MyTest
{

    //水印类
     public static class Water
    {
        private const int EM_SETCUEBANNER = 0x1501;
        [DllImport("user32.dll", CharSet = CharSet.Auto)]

        private static extern Int32 SendMessage
     (IntPtr hWnd, int msg, int wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);

        /// <summary>
        /// 为文本框设置水印文字
        /// </summary>
        /// <param name="textbox"></param>
        /// <param name="watermark"></param>
        public static void setWatermark(this TextBox textbox, string watermark)
        {
            SendMessage(textbox.Handle, EM_SETCUEBANNER, 0, watermark);
        }

        //清除水印方法
        public static void ClearWatermark(this TextBox textbox)
        {
            SendMessage(textbox.Handle, EM_SETCUEBANNER, 0, string.Empty);
        }
    }
}
